<template>
  <div class="app-container">
    <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input clearable v-model="searchForm.keyword" placeholder="搜索"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button title="添加" type="success" @click="$router.push({ name: 'goodsCategoryForm' })">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      row-key="uniqid"
      highlight-current-row
      :load="load"
      lazy
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column align="center" label="#" width="120" prop="category_id"/>

      <el-table-column label="图标" width="128" align="center">
        <template v-slot:default="{scope,row}">
          <el-image :src="row.image" :preview-src-list="[row.image]" :preview-teleported="true"
                    style="width: 28px;height: 28px;"
          />
        </template>
      </el-table-column>

      <el-table-column label="名称" width="180" prop="name"/>

      <el-table-column label="状态" width="100">
        <template v-slot:default="{scope,row}">
          <el-switch
            v-model="row.status"
            :active-value="1"
            :inactive-value="-1"
            @change="changeStatus(row.category_id)"
          />
        </template>
      </el-table-column>


      <el-table-column align="center" label="操作" width="180">
        <template v-slot:default="{scope,row}">
          <el-button type="success" size="mini" icon="el-icon-edit" circle
                     @click="$router.push({ name: 'goodsCategoryForm' , query: { id: row.category_id }})"
          ></el-button>
          <el-button v-if="!row.is_category" type="info" size="mini" icon="el-icon-document-copy" title="复制" circle
                     @click="copy(row.category_id)"
          ></el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" circle @click="del(row.category_id)"></el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>

</template>

<script>
import api from '@/api/category'

export default {
  components: {},
  filters: {},
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {},
      searchForm: {
        keyword: '',
        from: ''
      }
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    load(tree, treeNode, resolve) {
      api.getList({ category_id: tree.category_id, getChild: 1 }).then(response => {
        setTimeout(() => {
          resolve(response.data.list)
        }, 100)
      })
    },
    handleCategorySelect(category) {
      this.searchForm.category_id = category
    },
    fetchData() {
      this.listLoading = true
      api.getList(this.listQuery).then(response => {
        this.list = response.data.data
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.page = 1
      this.listQuery = Object.assign(this.listQuery, this.searchForm)
      this.fetchData()
    },
    del(id) {
      this.$confirm('此操作将删除此分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.del({ id: id }).then(res => {
          this.$message({
            type: 'success',
            message: res.msg
          })
          this.fetchData()
        })
      }).catch(() => {

      })
    },
    copy(id) {
      this.$confirm('是否复制该数据?, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.copy({ id: id }).then((res) => {
          this.fetchData()
          this.$message({
            type: 'success',
            message: '复制成功!'
          })
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消复制'
        })
      })
    },
    changeStatus(id) {
      api.status({ id: id })
        .then(res => {
          this.$message({
            message: res.msg,
            type: 'success'
          })
        })
    }
  }

}
</script>
